package com.fxd.core.util;

import com.google.common.base.MoreObjects;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class R<T> {

    private static final long serialVersionUID = 1L;

    private String code = "200";

    private String msg;

    private T data;

    public R() {
    }

    public R(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public R(T data) {
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> R<T> ok(T data) {
        return new R<>(data);
    }

    public static R ok() {
        return new R<>();
    }

    public static R fail(String code, String msg) {
        return new R<>(code, msg);
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .add("code", code)
                .add("msg", msg)
                .add("data", data == null ? "" : ToStringBuilder.reflectionToString(data, ToStringStyle.JSON_STYLE))
                .toString();
    }
}
